package com.ppt.android.base.utils

import android.annotation.SuppressLint
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*
import kotlin.math.abs

/**
 * 时间工具类
 * ________________________________________________________________________________________
 * |字母	|日期或时间元素	       | 表示	           | 示例                                  |
 * |:--:|:--------------------:|:-----------------:|:------------------------------------:|
 * |G	|Era 标志符	           | Text	           | AD                                   |
 * |y	|年	                   | Year              | 1996; 96                             |
 * |M	|年中的月份	           | Month	           | July; Jul; 07                        |
 * |w	|年中的周数	           | Number            | 27                                   |
 * |W	|月份中的周数	           | Number            | 2                                    |
 * |D	|年中的天数	           | Number            | 189                                  |
 * |d	|月份中的天数	           | Number            | 10                                   |
 * |F	|月份中的星期	           | Number            | 2                                    |
 * |E	|星期中的天数	           | Text	           | Tuesday; Tue                         |
 * |a	|Am/pm 标记	           | Text	           | PM                                   |
 * |H	|一天中的小时数(0-23)    |  Number            | 0                                    |
 * |k	|一天中的小时数(1-24）   |  Number            | 24                                   |
 * |K	|am/pm 中的小时数(0-11) |  Number            | 0                                    |
 * |h	|am/pm 中的小时数(1-12) |  Number            | 12                                   |
 * |m	|小时中的分钟数	       | Number            | 30                                   |
 * |s	|分钟中的秒数	           | Number            | 55                                   |
 * |S	|毫秒数	               | Number            | 978                                  |
 * |z	|时区	               | General time zone | Pacific Standard Time; PST; GMT-08:00|
 * |Z	|时区	               | RFC 822 time zone | -0800                                |
 * ￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣
 * @author ZouJiaCai
 * @since 2020/9/8
 */
object DateUtils {

    /**
     * 获取时间格式化String
     * @param timestamp 时间戳
     * @param dateFormat 日期格式
     * @return 格式化后的字符串
     */
    fun getDateFormatString(timestamp: Long, dateFormat: String): String =
        SimpleDateFormat(dateFormat, Locale.CHINESE).format(Date(timestamp))

    /**
     * 将固定格式[dateFormat]的时间字符串[dateString]转换为时间值
     */
    fun getDateStringToDate(dateString: String, dateFormat: String): Long? {
        val simpleDateFormat = SimpleDateFormat(dateFormat, Locale.getDefault())
        var date: Date? = null
        try {
            date = simpleDateFormat.parse(dateString)
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        return date?.time
    }

    /**
     * 将计时毫秒值[millisecond]转换为时分秒
     */
    fun getGapTime2(millisecond: Long): String {
        val days = millisecond / (1000 * 60 * 60 * 24)
        val hours = (millisecond - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
        val minutes = (millisecond - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60)
        val second = (millisecond  - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / 1000
        var diffTime: String
        diffTime = String.format("%s天", days)
        if(hours < 10){
            diffTime += String.format("0%s时", hours)
        }else{
            diffTime += String.format("%s时", hours)
        }
        if (minutes < 10) {
            diffTime += String.format("0%s分", minutes)
        }else{
            diffTime += String.format("%s分", minutes)
        }
        if (second < 10) {
            diffTime += String.format("0%s秒", second)
        }else{
            diffTime += String.format("%s秒", second)
        }
        return diffTime
    }

    /**
     * 将计时毫秒值[millisecond]转换为时分秒
     */
    fun getGapTime(millisecond: Long): String {
        val hours = millisecond / (1000 * 60 * 60)
        val minutes = (millisecond - hours * (1000 * 60 * 60)) / (1000 * 60)
        val second = (millisecond - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / 1000
        var diffTime: String
        diffTime = if (minutes < 10) {
            "$hours:0$minutes"
        } else {
            "$hours:$minutes"
        }
        diffTime = if (second < 10) {
            "$diffTime:0$second"
        } else {
            "$diffTime:$second"
        }
        return diffTime
    }

    /**
     * 获取以当前日期为基准的某一时间段的日期
     * @param isFuture Boolean 真为未来时间 假为以前的时间
     * @param interval Int 间隔时间 以当前时间为基准 距今天前n天或后n天开始 就是n 0是当前日期
     * @param size String 时间区间长度  比如获取五天的时间 就是5 当前日期也算一天
     * @return List<String> 日期集合 顺序为日期的新旧程度
     * @throws RuntimeException 如果[interval]小于0或者[size]小于1会抛出[RuntimeException]
     *
     * 示例：获取后天开始 为期七天的时间就是 getExcerptDate(true, 2, 7)
     *      获取昨天开始再往前7天的时间 getExcerptDate(false, 1, 7)
     */
    fun getExcerptDate(
        isFuture: Boolean,
        interval: Int,
        size: Int,
        dateFormat: String
    ): List<String> {
        if (interval < 0) throw RuntimeException("\"interval\" it can't be less than 0")
        if (size < 1) throw RuntimeException("\"size\" it can't be less than 1")
        val simpleDateFormat = SimpleDateFormat(dateFormat, Locale.CHINESE)
        val calendar = Calendar.getInstance()
        val currentDayOfYear = calendar.get(Calendar.DAY_OF_YEAR)
        val currentYear = calendar.get(Calendar.YEAR)
        val dateList = mutableListOf<String>()
        if (isFuture) {
            (interval until interval + size).forEach {
                val timestamp = getSomedayDate(it, calendar, currentDayOfYear, currentYear)
                dateList.add(simpleDateFormat.format(timestamp))
            }
        } else {
            (-interval downTo -interval - size + 1).forEach {
                val timestamp = getSomedayDate(it, calendar, currentDayOfYear, currentYear)
                dateList.add(simpleDateFormat.format(timestamp))
            }
        }
        return dateList
    }

    /**
     * 获取距离今天的某一天的时间戳
     * @param numberOfDaysBetween Int 间隔今天的天数 正数为未来时间 负数为以前的时间
     * @param calendar Calendar Calendar对象 使用依赖注入方式 提高对象的复用性
     * @param currentDayOfYear Int 当前时间在当年的天 使用Calendar获取
     * @param currentYear Int 当前年 使用Calendar获取
     * @return Long 时间戳
     */
    fun getSomedayDate(
        numberOfDaysBetween: Int,
        calendar: Calendar,
        currentDayOfYear: Int,
        currentYear: Int
    ): Long {
        calendar.set(Calendar.DAY_OF_YEAR, currentDayOfYear)
        calendar.set(Calendar.YEAR, currentYear)
        calendar.set(
            Calendar.DAY_OF_YEAR,
            calendar.get(Calendar.DAY_OF_YEAR) + numberOfDaysBetween
        )
        return calendar.time.time
    }

    /**
     * String 转化 Calendar
     * @param string String
     * @param format String
     */
    fun stringToCalendar(string: String, format: String): Calendar? {
        val sdf = SimpleDateFormat(format, Locale.CHINESE)
        var calendar: Calendar
        try {
            val date: Date = sdf.parse(string) ?: return null
            calendar = Calendar.getInstance()
            calendar.time = date
        } catch (e: ParseException) {
            e.printStackTrace()
            calendar = Calendar.getInstance()
        }
        return calendar
    }

    fun dateToCalendar(date: Long): Calendar {
        var calendar: Calendar
        try {
            calendar = Calendar.getInstance()
            calendar.time = Date(date)
        } catch (e: ParseException) {
            e.printStackTrace()
            calendar = Calendar.getInstance()
        }
        return calendar
    }

    /**
     * String 转化Date
     * @param str String
     * @param format String
     * @return Date
     */
    fun strToDate(str: String, format: String): Date? {
        val sdf = SimpleDateFormat(format, Locale.CHINESE)
        return try {
            sdf.parse(str)
        } catch (e: ParseException) {
            e.printStackTrace()
            null
        }
    }

    /**
     * 判断两个时间是否是同一天
     * @param cal1 Calendar
     * @param cal2 Calendar
     * @return Boolean
     */
    fun isSameDay(cal1: Calendar, cal2: Calendar): Boolean {
        return cal1.get(Calendar.YEAR) == cal2[Calendar.YEAR] && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH) && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR)
    }

    @SuppressLint("SimpleDateFormat")
    fun isSameDay(startDate: Date,endDate: Date): Boolean {
        val fmt = SimpleDateFormat("yyyyMMdd")
        LogUtils.d("开始日期：${fmt.format(startDate)}","结束日期：${fmt.format(endDate)}")
        return fmt.format(startDate) == (fmt.format(endDate))
    }

    fun formatAMPMTime(time: String): String{
        val displayFormat = SimpleDateFormat("HH:mm")
        val parseFormat = SimpleDateFormat("hh:mm a",Locale.ENGLISH)
        val date = parseFormat.parse(time)//"10:30 PM"
//        println(parseFormat.format(date) + " = " + displayFormat.format(date))//10:30 PM = 22:30
        return displayFormat.format(date)
    }

    /***
     * 计算相差天数
     */
    fun diffDays(startDate: Long, endDate: Long): Long {
        return abs(((endDate - startDate)) / (24 * 60 * 60 * 1000))
    }

    /**
     * 返回倒计时格式: 23:59:59
     */
    fun getCountDownText(millis: Long): String {
        val sb = StringBuilder()
        val day = Math.floor((millis / 3600 / 24).toDouble()).toInt()
        val yday = millis.toInt() % (24 * 60 * 60)
        val hours = Math.floor((yday / 60 / 60).toDouble()).toInt()
        val ys = yday % (60 * 60)
        val minutes = Math.floor((ys / 60).toDouble()).toInt()
        val seconds = ys % 60
        if (hours < 10) {
            sb.append("0").append(hours)
        } else {
            sb.append(hours)
        }
        sb.append(":")
        if (minutes < 10) {
            sb.append("0").append(minutes)
        } else {
            sb.append(minutes)
        }
        sb.append(":")
        if (seconds < 10) {
            sb.append("0").append(seconds)
        } else {
            sb.append(seconds)
        }
        return sb.toString()
    }

    /*
    * 获取上月第一天的date
    */
    fun getLastMonthFirstDate(): Long? {
        val c = Calendar.getInstance()
        c.add(Calendar.MONTH, -1)
        val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        val gtimelast = sdf.format(c.time) //上月
//        println(gtimelast)
        val lastMonthMaxDay = c.getActualMaximum(Calendar.DAY_OF_MONTH)
//        println(lastMonthMaxDay)
        c[c[Calendar.YEAR], c[Calendar.MONTH], lastMonthMaxDay, 23, 59] = 59

        //按格式输出
        val gtime = sdf.format(c.time) //上月最后一天

//        LogUtils.d("上月最后一天",gtime)
        val sdf2 = SimpleDateFormat("yyyy-MM-01 00:00:00")
        val gtime2 = sdf2.format(c.time) //上月第一天

//        LogUtils.d("上月第一天",gtime2)
        return getDateStringToDate(gtime2,"yyyy-MM-dd HH:mm:ss")
    }

    /*
    * 获取上月最后一天的date
    */
    fun getLastMonthLastDate(): Long?{
        val c = Calendar.getInstance()
        c.add(Calendar.MONTH, -1)
        val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        val gtimelast = sdf.format(c.time) //上月
//        println(gtimelast)
        val lastMonthMaxDay = c.getActualMaximum(Calendar.DAY_OF_MONTH)
//        println(lastMonthMaxDay)
        c[c[Calendar.YEAR], c[Calendar.MONTH], lastMonthMaxDay, 23, 59] = 59

        //按格式输出
        val gtime = sdf.format(c.time) //上月最后一天

//        LogUtils.d("上月最后一天",gtime)
        val sdf2 = SimpleDateFormat("yyyy-MM-01 00:00:00")
        val gtime2 = sdf2.format(c.time) //上月第一天

//        LogUtils.d("上月第一天",gtime2)
        return getDateStringToDate(gtime,"yyyy-MM-dd HH:mm:ss")
    }

    /**
     * 获取本月的第一天
     */
    fun getCurMonthFirstDate(): Long? {
        val c = Calendar.getInstance()
        val sdf2 = SimpleDateFormat("yyyy-MM-01 00:00:00")
        val gtime2 = sdf2.format(c.time)
        return getDateStringToDate(gtime2,"yyyy-MM-dd HH:mm:ss")
    }

    /**
     * 获取昨天开始时间和结束时间
     */
    fun getYesterdayRange(): LinkedHashMap<String, Long?> {
        val map: LinkedHashMap<String,Long?> = LinkedHashMap()
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.MILLISECOND,0)
        calendar.set(Calendar.SECOND,0)
        calendar.set(Calendar.MINUTE,0)
        calendar.set(Calendar.HOUR_OF_DAY,0)
        calendar.set(Calendar.HOUR_OF_DAY,-24)
        LogUtils.d("start_time", getDateFormatString(calendar.time.time,"yyyy-MM-dd HH:mm:ss"))
        map["start_time"] = calendar.time.time

        calendar.set(Calendar.MILLISECOND,0)
        calendar.set(Calendar.SECOND,59)
        calendar.set(Calendar.MINUTE,59)
        calendar.set(Calendar.HOUR_OF_DAY,23)
        LogUtils.d("end_time",getDateFormatString(calendar.time.time,"yyyy-MM-dd HH:mm:ss"))
        map["end_time"] = calendar.time.time
        return map
    }

    /**
     *  获取今日开始时间和结束时间
     */
    fun getDayRange(timestamp: Long? = null): LinkedHashMap<String, Long?>{
        val map: LinkedHashMap<String,Long?> = LinkedHashMap()
        val calendar = Calendar.getInstance()
        if (timestamp != null){
            calendar.time = Date(timestamp)
        }
        calendar.set(Calendar.MILLISECOND,0)
        calendar.set(Calendar.SECOND,0)
        calendar.set(Calendar.MINUTE,0)
        calendar.set(Calendar.HOUR_OF_DAY,0)
        map["start_time"] = calendar.time.time
        calendar.set(Calendar.HOUR_OF_DAY,24)
        calendar.set(Calendar.SECOND,-1)
        map["end_time"] = calendar.time.time
        return map
    }

    /**
     * 获取最近三天的开始时间和结束时间
     */
    fun getThreeRange(): LinkedHashMap<String, Long?>{
        val map: LinkedHashMap<String,Long?> = LinkedHashMap()
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.MILLISECOND,0)
        calendar.set(Calendar.SECOND,0)
        calendar.set(Calendar.MINUTE,0)
        calendar.set(Calendar.HOUR_OF_DAY,24)
        map["end_time"] = calendar.time.time
        calendar.set(Calendar.HOUR_OF_DAY,-72)
        map["start_time"] = calendar.time.time
        return map
    }

    /**
     * 获取近一周的开始时间和结束时间
     */
    fun getDaySevenRange(): LinkedHashMap<String, Long?>{
        val map: LinkedHashMap<String,Long?> = LinkedHashMap()
        val calendar = Calendar.getInstance()
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH),0,0,0)
        calendar.set(Calendar.HOUR_OF_DAY,24)
        map["end_time"] = calendar.time.time
        calendar.set(Calendar.HOUR_OF_DAY,-168)
        map["start_time"] = calendar.time.time
        return map
    }

    /**
     * 获取近一个月的开始时间和结束时间
     */
    fun getDayTRange(): LinkedHashMap<String, Long?>{
        val map: LinkedHashMap<String,Long?> = LinkedHashMap()
        val calendar = Calendar.getInstance()
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH),0,0,0)
        calendar.set(Calendar.HOUR_OF_DAY,24)
        map["end_time"] = calendar.time.time
        calendar.set(Calendar.HOUR_OF_DAY,-720)
        map["start_time"] = calendar.time.time
        return map
    }

    /**
     * 获取本周开始时间和结束时间
     */
    fun getWeekRange(): LinkedHashMap<String, Long?>{
        val map: LinkedHashMap<String,Long?> = LinkedHashMap()
        val calendar = Calendar.getInstance()
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH),0,0,0)
        calendar.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY)
        map["start_time"] = calendar.time.time
        calendar.add(Calendar.DAY_OF_WEEK,7)
        calendar.add(Calendar.SECOND,-1)
        map["end_time"] = calendar.time.time
        return map
    }

    /**
     * 获取上周开始时间和结束时间
     */
    fun getLastWeekRange(): LinkedHashMap<String, Long?>{
        val map: LinkedHashMap<String,Long?> = LinkedHashMap()
        val calendar = Calendar.getInstance()
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH),0,0,0)
        calendar.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY)
        calendar.add(Calendar.DAY_OF_WEEK,-7)
        map["start_time"] = calendar.time.time
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH),23,59,59)
        calendar.add(Calendar.DAY_OF_WEEK,6)
        map["end_time"] = calendar.time.time
        return map
    }

    /**
     * 获取本月开始时间和结束时间
     */
    fun getMonthRange(): LinkedHashMap<String, Long?>{
        val map: LinkedHashMap<String,Long?> = LinkedHashMap()
        val calendar = Calendar.getInstance()
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH),0,0,0)
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DAY_OF_MONTH))
        map["start_time"] = calendar.time.time
        calendar.add(Calendar.DAY_OF_WEEK,7)
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH))
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH),23,59,59)
        map["end_time"] = calendar.time.time
        return map
    }

    /**
     * 获取上月开始时间和结束时间
     */
    fun getLastMonthRange(): LinkedHashMap<String, Long?>{
        val map: LinkedHashMap<String,Long?> = LinkedHashMap()
        val calendar = Calendar.getInstance()
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH),0,0,0)
        calendar.add(Calendar.MONTH,-1)
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH))
        map["start_time"] = calendar.time.time
        calendar.add(Calendar.DAY_OF_WEEK,7)
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH))
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH),23,59,59)
        map["end_time"] = calendar.time.time
        return map
    }

    /**
     * 获取本季度开始时间和结束时间
     */
    fun getQuarterRange(): LinkedHashMap<String, Long?>{
        val map: LinkedHashMap<String,Long?> = LinkedHashMap()
        val calendar = Calendar.getInstance()
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH),0,0,0)
        val currentMonth = calendar.get(Calendar.MONTH)+1
        try {
            when (currentMonth) {
                in 1..3 -> calendar.set(Calendar.MONTH,0)
                in 4..6 -> calendar.set(Calendar.MONTH,3)
                in 7..9 -> calendar.set(Calendar.MONTH,6)
                in 10..12 -> calendar.set(Calendar.MONTH,9)
            }
            calendar.set(Calendar.DATE,1)
        }catch (e: Exception){
            e.printStackTrace()
        }
        map["start_time"] = calendar.time.time
        calendar.add(Calendar.MONTH,3)
        calendar.set(Calendar.SECOND,-1)
        map["end_time"] = calendar.time.time
        return map
    }

    /**
     * 获取上季度开始时间和结束时间
     */
    fun getLastQuarterRange(): LinkedHashMap<String, Long?>{
        val map: LinkedHashMap<String,Long?> = LinkedHashMap()
        val calendar = Calendar.getInstance()
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH),0,0,0)
        val currentMonth = calendar.get(Calendar.MONTH)+1
        try {
            when (currentMonth) {
                in 1..3 -> calendar.set(Calendar.MONTH,0)
                in 4..6 -> calendar.set(Calendar.MONTH,3)
                in 7..9 -> calendar.set(Calendar.MONTH,6)
                in 10..12 -> calendar.set(Calendar.MONTH,9)
            }
            calendar.set(Calendar.DATE,1)
        }catch (e: Exception){
            e.printStackTrace()
        }
        calendar.add(Calendar.MONTH,-3)
        map["start_time"] = calendar.time.time
        calendar.add(Calendar.MONTH,3)
        calendar.set(Calendar.SECOND,-1)
        map["end_time"] = calendar.time.time
        return map
    }

    /**
     * 获取近半年开始时间和结束时间
     */
    fun getHalfYearRange(): LinkedHashMap<String, Long?>{
        val map: LinkedHashMap<String,Long?> = LinkedHashMap()
        val calendar = Calendar.getInstance()
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH),0,0,0)
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DAY_OF_MONTH))
        calendar.add(Calendar.MONTH,-6)
        map["start_time"] = calendar.time.time
        calendar.add(Calendar.MONTH,6)
        calendar.set(Calendar.SECOND,-1)
        map["end_time"] = calendar.time.time
        return map
    }

    /**
     * 获取上半年开始时间和结束时间
     */
    fun getUpHalfYearRange(): LinkedHashMap<String, Long?>{
        val map: LinkedHashMap<String,Long?> = LinkedHashMap()
        val calendar = Calendar.getInstance()
        calendar.set(calendar.get(Calendar.YEAR),0,
            calendar.get(Calendar.DAY_OF_MONTH),0,0,0)
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DAY_OF_MONTH))
        map["start_time"] = calendar.time.time
        calendar.add(Calendar.MONTH,6)
        calendar.set(Calendar.SECOND,-1)
        map["end_time"] = calendar.time.time
        return map
    }

    /**
     * 获取下半年开始时间和结束时间
     */
    fun getNextHalfYearRange(): LinkedHashMap<String, Long?>{
        val map: LinkedHashMap<String,Long?> = LinkedHashMap()
        val calendar = Calendar.getInstance()
        calendar.set(calendar.get(Calendar.YEAR),0,
            calendar.get(Calendar.DAY_OF_MONTH),0,0,0)
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DAY_OF_MONTH))
        calendar.add(Calendar.MONTH,6)
        map["start_time"] = calendar.time.time
        calendar.add(Calendar.MONTH,6)
        calendar.set(Calendar.SECOND,-1)
        map["end_time"] = calendar.time.time
        return map
    }

    /**
     * 获取本年开始时间和结束时间
     */
    fun getYearRange(): LinkedHashMap<String, Long?>{
        val map: LinkedHashMap<String,Long?> = LinkedHashMap()
        val calendar = Calendar.getInstance()
        calendar.set(calendar.get(Calendar.YEAR),0,
            calendar.get(Calendar.DAY_OF_MONTH),0,0,0)
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DAY_OF_MONTH))
        map["start_time"] = calendar.time.time
        calendar.add(Calendar.YEAR,1)
        calendar.set(Calendar.SECOND,-1)
        map["end_time"] = calendar.time.time
        return map
    }

    /**
     * 获取上一年开始时间和结束时间
     */
    fun getLastYearRange(): LinkedHashMap<String, Long?>{
        val map: LinkedHashMap<String,Long?> = LinkedHashMap()
        val calendar = Calendar.getInstance()
        calendar.set(calendar.get(Calendar.YEAR),0,
            calendar.get(Calendar.DAY_OF_MONTH),0,0,0)
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DAY_OF_MONTH))
        calendar.add(Calendar.YEAR,-1)
        map["start_time"] = calendar.time.time
        calendar.add(Calendar.YEAR,1)
        calendar.set(Calendar.SECOND,-1)
        map["end_time"] = calendar.time.time
        return map
    }


    /**
     * 返回友好时间跨度
     *
     * @param date 需要格式化的时间
     *
     * @return the fit time span
     *         return 小于1分钟，返回"刚刚"
     *         return 小于1小时但大于0分钟，返回"X分钟前"
     *         return 小于1天但大于0小时，返回"X小时前"
     *         return 昨天，返回"昨天"
     *         return 大于1天，返回"X天前"
     */
    fun calculateTimeDifference(date:Date): String {
        val currentTime = System.currentTimeMillis()
        val timeDifference = currentTime - date.time
        // 计算时间差对应的单位
        val seconds = timeDifference / 1000
        val minutes = seconds / 60
        val hours = minutes / 60
        val days = hours / 24

        if (days > 1) {
            // 大于1天，返回"X天前"
            return days.toString() + "天前"
        } else if (days.toInt() == 1) {
            // 昨天，返回"昨天"
            return "昨天"
        } else if (hours > 0) {
            // 小于1天但大于0小时，返回"X小时前"
            return hours.toString() + "小时前"
        } else if (minutes > 0) {
            // 小于1小时但大于0分钟，返回"X分钟前"
            return minutes.toString() + "分钟前"
        } else {
            // 小于1分钟，返回"刚刚"
            return "刚刚"
        }
    }
}